package com.eastfair.properties;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.AntPathMatcher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 忽略token 配置类
 * <p>
 * 做接口权限时，考虑修改成读取配置文件
 *
 * @author ligang
 * @date 2019/01/03
 */
@Data
public class IgnoreProperties {
    private static final AntPathMatcher ANT_PATH_MATCHER = new AntPathMatcher();

    private static List<String> baseUri = Arrays.asList(
            "/**/*.css",
            "/**/*.js",
            "/**/*.html",
            "/**/*.ico",
            "/**/*.jpg",
            "/**/*.jpeg",
            "/**/*.png",
            "/**/*.gif",
            "/**/v2/**",
            "/**/api-docs/**",
            "/**/api-docs-ext/**",
            "/**/swagger-resources/**",
            "/**/webjars/**",
            "/actuator/**",
            "/error",
            "/**/anno/**",
            "/**/static/**",


            "/kaptcha/imgKaptcha",
            "/kaptcha/imgvrify",


            "/auth/userAccount/register",
            "/auth/userAccount/registerSubSystemUser",
            "/auth/userAccount/login",
            "/auth/userAccount/findUser",
            "/auth/userAccount/queryOfPhoneOrEmail",
            "/auth/userAccount/loginOfMobile",
            "/auth/userAccount/loginOfEmail",
            "/auth/userAccount/getTokenOfMini",
            "/auth/userAccount/updPasswordOfCode",

            "/auth/userAccount/mobileCode",
            "/auth/userAccount/iamLogin",
            "/auth/userAccount/getToken",

            "/auth/iam/**/**",

            "/project-core/kaptcha/imgKaptcha",
            "/project-core/kaptcha/imgvrify",
            "/project-core/projectDb/createAdminProject",
            "/file/upload/local-put-file",
            "/venue-services-manage/register/user",
            "/matching/complementRegister/registerComplementUser",
            "/home/homeRegister/registerHomeUser",

            "/builder/builderRegister/registerBuilderUser",
            "/main-transport/transportRegister/registerUser",

            "/common-service/roadGate/inOff/sync",
            "/common-service/logistics/blacklist/sync",
            "/common-service/authCode/get/authUserId",
            "/common-service/authCodeUser/bind",
            "/venue-services-manage/exhibitionManage/queryAllExhibitionManageForExternal",
            "/venue-services-manage/exhibitionManage/queryExhibitionManageForExternal",

            "/messagecenter/msg/executeSend",

            "/certificate/certificateUser/sycnECertificateUser",

            "/venue-booking/exhibition/syncWebsiteExhibition"
//            "/project-core/projectDb/updAdminProject"
    );

    private static List<String> tenant = CollUtil.newArrayList("/**/noTenant/**");
    private static List<String> token = CollUtil.newArrayList("/**/noToken/**", "/ds/**");

    public static boolean isIgnoreToken(String path) {
        List<String> all = new ArrayList<>();
        all.addAll(IgnoreProperties.baseUri);
        all.addAll(token);
//        return all.stream().anyMatch(url -> path.startsWith(url) || ANT_PATH_MATCHER.match(url, path));
        return all.stream().anyMatch(url -> (path.startsWith(url) && isEnd(path, url)) || ANT_PATH_MATCHER.match(url, path));
//        return all.contains(path);
    }

    public static boolean isEnd(String path, String url) {
        if (path.substring(path.lastIndexOf("/")).equals(url.substring(url.lastIndexOf("/")))) {
            return true;
        }
        return false;
    }

    public static boolean isContain(List<String> all, String path) {

        return all.stream().anyMatch(url -> path.startsWith(url) || ANT_PATH_MATCHER.match(url, path));
    }


    public static boolean isIgnoreTenant(String path) {
        List<String> all = new ArrayList<>();
        all.addAll(IgnoreProperties.baseUri);
        all.addAll(tenant);
        return all.stream().anyMatch(url -> path.startsWith(url) || ANT_PATH_MATCHER.match(url, path));
    }

    public static String[] listToStr() {
        String[] strings = new String[baseUri.size()];
        baseUri.toArray(strings);
        return strings;
    }


}
